package com.lanou3g.ptp.utils;

import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {

    public static Date getTodayDateIgnoreTime() {
        return getCalendarIgnoreTime().getTime();
    }

    public static Date getTomorrowDateIgnoreTime() {
        Calendar calendarIgnoreTime = getCalendarIgnoreTime();
        calendarIgnoreTime.add(Calendar.DATE, 1);
        return calendarIgnoreTime.getTime();
    }

    private static Calendar getCalendarIgnoreTime(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal;
    }

    private static Calendar getCalendarIgnoreTime() {
        return getCalendarIgnoreTime(new Date());
    }

    public static boolean isToday(Date date) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");//隨便寫一種日期格式
        String dateStr = sf.format(date);//需要判定的时间
        String todayStr = sf.format(new Date()); //当前时间
        return todayStr.equals(dateStr);

    }


    //判定是不是昨天
    public static boolean isYesterday(Date date) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();//生成当前时间
        calendar.add(Calendar.DATE, -1);
        String yesterdayStr = sf.format(calendar.getTime());  //获取昨天日期
        String dateStr = sf.format(date);//需要判定的时间
        return yesterdayStr.equals(dateStr);
    }

    public static boolean isTomrrow(Date date) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();//生成当前时间
        calendar.add(Calendar.DATE, 1);
        String tomStr = sf.format(calendar.getTime());  //获取昨天日期
        String dateStr = sf.format(date);//需要判定的时间
        return tomStr.equals(dateStr);
    }

    public static Date now() {
        return new Date();
    }

    public static boolean isSameDay(Calendar base, Calendar other) {
        return base.get(Calendar.YEAR) == other.get(Calendar.YEAR)
                && base.get(Calendar.MONTH) == other.get(Calendar.MONTH)
                && base.get(Calendar.DATE) == other.get(Calendar.DATE);
    }

    public static boolean isSameDay(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        return isSameDay(cal1, cal2);
    }

    public static List<Date> getWeekDates() {
        return getWeekDates(getTodayDateIgnoreTime());
    }

    public static List<Date> getWeekDates(Date date) {
        List<Date> dates = new ArrayList<>();
        for (int i = 1; i <= 7; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int day = calendar.get(Calendar.DAY_OF_WEEK);
            calendar.add(Calendar.DAY_OF_WEEK, i - day + 1);
            dates.add(calendar.getTime());
        }
        return dates;
    }

    public static Date startTime(Date date ){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }

    public static Date endTime(Date date ){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        calendar.set(Calendar.MILLISECOND,999);
        return calendar.getTime();
    }

}
